package com.sunlgggg.view.component;

import com.sunlgggg.view.MainFrame;

import javax.swing.*;
import java.awt.*;

import static com.sunlgggg.util.HttpRequestUtils.*;

/**
 *
 * Created by sunlggggg on 2016/12/1.
 */
public class ResetPanel extends JPanel {




    JButton jButton = new JButton("确定");
    public ResetPanel() {


        this.setLayout(new BorderLayout());
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout());
        JPanel mainPanel = new JPanel();

        System.out.println(mainPanel.getHeight());
        mainPanel.setLayout(new GridLayout(4,1,100,100));

        JPanel oldPanel = new JPanel();
        oldPanel.setLayout(new GridLayout(1,2));

        JLabel oldjLabel = new JLabel("请输入旧密码");
        JPasswordField oldJTextField = new JPasswordField();
        oldPanel.add(oldjLabel);
        oldPanel.add(oldJTextField);


        JPanel newPanel = new JPanel();
        newPanel.setLayout(new GridLayout(1,2));
        JLabel newjLabel = new JLabel("请输入新密码");
        JPasswordField newJTextField = new JPasswordField();
        newPanel.add(newjLabel);
        newPanel.add(newJTextField);


        JPanel repeatPanel = new JPanel();


        repeatPanel.setLayout(new GridLayout(1,2));
        JLabel repeatjLabel = new JLabel("请输入重复密码");
        JPasswordField repeatJTextField = new JPasswordField();
        repeatPanel.add(repeatjLabel);
        repeatPanel.add(repeatJTextField);

        mainPanel.add(oldPanel,BorderLayout.NORTH);
        mainPanel.add(newPanel,BorderLayout.CENTER);
        mainPanel.add(repeatPanel,BorderLayout.NORTH);

        mainPanel.add(jButton);

        titlePanel.setBorder(BorderFactory.createEmptyBorder(40, 0, 0, 0));
        JLabel title = new JLabel("修改密码", SwingConstants.CENTER);
        title.setFont(new Font("宋体", Font.BOLD, 28));
        titlePanel.add(title);
        this.add(titlePanel, BorderLayout.NORTH);
        this.add(mainPanel,BorderLayout.CENTER);

        jButton.addActionListener(e -> {
            if (!repeatJTextField.getText().equals(newJTextField.getText())){
                JOptionPane.showMessageDialog(new JFrame(), "两次密码不同！");
                return;
            }

            int n = JOptionPane.showConfirmDialog(null, "确认修改吗?", "确认对话框", JOptionPane.YES_NO_OPTION);
            if (n == JOptionPane.YES_OPTION) {

                String ret = post("user/modify", "account="+ MainFrame.currentAccount +"&password=" + repeatJTextField.getText());

                if (ret.equals("success")){
                    JOptionPane.showMessageDialog(new JFrame(), "修改成功");
                }else {
                    JOptionPane.showMessageDialog(new JFrame(), "修改失败");
                }
            }
        });
    }


}
